package com.lihaozhe.util.pagination;

import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * @author 李昊哲
 * @Description
 * @createTime 2021/7/9 上午10:52
 */
@Setter
@Getter
public class Pager<E> {
    /**
     * 查询页码
     */
    private int pageNum;
    /**
     * 每页记录数
     */
    private int pageSize;
    /**
     * 总记录数
     */
    private int total;
    /**
     * 总页码数
     */
    private int totalPage;
    /**
     * 当前页索引
     */
    private int pageIndex;
    /**
     * 是否首页
     */
    private boolean isFirstPage;
    /**
     * 是否末页
     */
    private boolean isLastPage;
    /**
     * 分页后的数据
     */
    private List<E> data;

    public Pager() {
        this(1, 5);
    }

    public Pager(int pageNum, int pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.pageIndex = (pageNum - 1) * pageSize;
    }

    public Pager(int pageNum, int pageSize, List<E> list) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.pageIndex = (pageNum - 1) * pageSize;
        this.total = list.size();
        this.totalPage = list.size() % pageSize == 0 ? list.size() / pageSize : (list.size() / pageSize) + 1;
        this.isFirstPage = pageNum <= 1 ? true : false;
        this.isLastPage = pageNum >= (list.size() % pageSize == 0 ? list.size() / pageSize : (list.size() / pageSize) + 1) ? true : false;
        this.data = this.isLastPage() ? list.subList((pageNum - 1) * pageSize, list.size()) : list.subList((pageNum - 1) * pageSize, pageNum * pageSize);
    }
}
